﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Forms;

namespace UCF.Core.Forms
{
    /// <summary>
    /// Dll加载器
    /// </summary>
    /// <remarks>2014-5-7 1.0 by sutroon</remarks>
    public class FormDllLoader
    {
        /// <summary>
        /// 打开一个窗体实例
        /// </summary>
        /// <param name="dllPath"></param>
        /// <param name="typeName"></param>
        /// <param name="properties"></param>
        /// <param name="owner"></param>
        public static void Show(string dllPath, string typeName, Dictionary<string, object> properties = null, Action<Form> act = null, bool showDialog = false, bool isSingletom = false)
        {
            Type type;
            Form frm;
            if (isSingletom)
            {
                for (int i = 0; i < Application.OpenForms.Count; i++)
                {
                    if (Application.OpenForms[i].GetType().Name == typeName)
                    {
                        Application.OpenForms[i].Close();
                        System.Threading.Thread.Sleep(500);
                    }
                }
            }
            try
            {
                type = DllLoader.GetTypeFromFile(dllPath, typeName);
                frm = (Form)Activator.CreateInstance(type);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            List<string> msg = new List<string>();
            if (properties != null)
            {
                foreach (string n in properties.Keys)
                {
                    PropertyInfo pi = type.GetProperty(n);
                    if (pi == null)
                    {
                        msg.Add("成员 [{0}] null".Fmt(n));
                    }
                    else
                    {
                        pi.SetValue(frm, properties[n], null);
                    }
                }
            }
            if (msg.Count > 0) MessageBox.Show(msg.ToString(Environment.NewLine));
            if (act != null) act(frm);
            if (showDialog)
            {
                frm.ShowDialog();
            }
            else
            {
                frm.Show();
            }
        }

        /// <summary>
        /// 打开一个mdi窗体
        /// </summary>
        /// <param name="frm"></param>
        /// <param name="dllPath"></param>
        /// <param name="typeName"></param>
        /// <param name="properties"></param>
        /// <param name="act"></param>
        /// <param name="refresh"></param>
        public static void OpenMidiForm(Form frm, string dllPath, string typeName, Dictionary<string, object> properties = null, Action<Form> act = null, bool refresh = false)
        {
            Type type;
            try
            {
                type = DllLoader.GetTypeFromFile(dllPath, typeName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            foreach (Form f in frm.MdiChildren)
            {
                if (f.GetType() == type)
                {
                    if (refresh)
                    {
                        f.Close();
                    }
                    else
                    {
                        f.Activate();
                    }
                }
            }
            try
            {
                Form mdifrm = (Form)Activator.CreateInstance(type);
                if (mdifrm.IsDisposed) return;
                mdifrm.MdiParent = frm;
                mdifrm.Dock = DockStyle.Fill; List<string> msg = new List<string>();
                if (properties != null)
                {
                    foreach (string n in properties.Keys)
                    {
                        PropertyInfo pi = type.GetProperty(n);
                        if (pi == null)
                        {
                            msg.Add("成员 [{0}] null".Fmt(n));
                        }
                        else
                        {
                            pi.SetValue(frm, properties[n], null);
                        }
                    }
                }
                if (msg.Count > 0) MessageBox.Show(msg.ToString(Environment.NewLine));
                if (act != null) act(frm);
                mdifrm.Show();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
